package exhibition.app.nexd.cn.ui.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import butterknife.Bind;
import exhibition.app.nexd.cn.R;
import exhibition.app.nexd.cn.presenter.LoadingPresenter;
import exhibition.app.nexd.cn.ui.ivew.ILoadingActivityView;

public class LoadingActivity extends FullScreenActivity<LoadingPresenter> implements ILoadingActivityView {

    @Bind(R.id.loading_rl_anim)
    View loadingAnimLayout;

    @Bind(R.id.loading_tv_anim)
    TextView loadingTvAnim;

    GestureDetector pullGesture;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initPresenter() {
        presenter = new LoadingPresenter(this, this);
        presenter.init();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return pullGesture.onTouchEvent(event);
    }

    private void setAnim() {
        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(loadingTvAnim, "rotationX", 0, 360);
        rotateAnimator.setDuration(2000);
        rotateAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                overridePendingTransition(R.anim.top_pull_in, R.anim.top_pull_out);
                startActivity(new Intent(LoadingActivity.this, LoginActivity.class));
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        rotateAnimator.start();
    }

    @Override
    public void initView() {
        presenter.getHireFromDB();
        pullGesture = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if ((e1.getRawY() - e2.getRawY()) > 200) {
                    setAnim();
                    return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);

            }
        });
    }

    @Override
    public void showToast(String msg) {

    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Animation anim = AnimationUtils
                .loadAnimation(this, R.anim.show);
        loadingAnimLayout.startAnimation(anim);
    }
}

